<?php


namespace App\Models\CacheModel\live;


use App\Models\Api\BaseCache;
use think\Log;
use AstroTool\Cache\CacheManage;

class LiveRtcRecord extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_rtc_record';
    protected $cacheModel = 'live.rtc_record';
    protected $cacheModel2 = 'live.now_rtc';    // 当前直播 连麦信息

    /**
     * 申请连麦
     */
    public function addRtc($data, $isAddList = true)
    {
        try
        {
            $rtcId = $this->insertGetId($data);
            if ($isAddList)
            {   // 是否添加到连麦列表
                $this->listDataAdd($data['live_record_id'], [$data['uid'] => $rtcId]);
            }
        }
        catch (\Exception $e)
        {
            $rtcId = 0;
            Log::write("申请连麦写入数据失败{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()};", 'error');
        }

        return (int)$rtcId;
    }

    /**
     * rtc连麦申请列表移除
     * @param int $liveRecordId
     * @param int $uid
     * @throws \Exception
     */
    public function rtcListDel(int $liveRecordId, int $uid)
    {
        $this->listDataDel($liveRecordId, [$uid]);
    }

    /**
     * 更新连麦信息
     */
    public function updateRtcInfo($rtcId, $updateData)
    {

        try
        {
            $this->where('id', $rtcId)->update($updateData);
            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $rtcId)->setCache($updateData);
        }
        catch (\Exception $e)
        {
            Log::write("申请连麦写入数据失败{$e->getMessage()};File:{$e->getFile()};Line:{$e->getLine()};", 'error');
            return false;
        }

        return true;
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('id', $ids)->column('*', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    /**
     * 获取连麦申请列表ID
     * @param int $liveRecordId
     * @return array
     * @throws \Exception
     */
    public function getRtcUserList(int $liveRecordId)
    {
        $idsData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $liveRecordId)
            ->setPage(-1)
            ->setSort('asc')
            ->getListPageDataIds(function () use ($liveRecordId)
            {
                try
                {
                    $data = $this->where('live_record_id', $liveRecordId)->whereIn('status', [1, 2, 3])->column('id,uid', 'uid');

                    $resData = [];
                    foreach ($data as $uid => $id)
                    {
                        $resData[$uid] = ['id' => $id];
                    }
                    return $resData;
                }
                catch (\Exception $e)
                {
                    return [];
                }
            }, 'id', 0);

        return $idsData['data'];
    }

    /**
     * 是否申请连麦
     * @param $liveRecordId
     * @param $uid
     * @return int   返回申请连麦的 申请连麦ID
     * @throws \Exception
     */
    public function isRequestRtc($liveRecordId, $uid): int
    {
        $rtcList = $this->getRtcUserList($liveRecordId);
        return isset($rtcList[$uid]) ? (int)$rtcList[$uid] : 0;
    }

    /**
     * 保存当前连麦数据 到缓存
     * @param int $liveRecordId
     * @param array $data
     * @return bool
     */
    public function saveNowRtcDataToCache(int $liveRecordId, array $data)
    {
        try
        {
            CacheManage::getInstance()->setCacheConfig($this->cacheModel2, $liveRecordId)->setCache($data, 'details', false);
        }
        catch (\Exception $e)
        {
            Log::write('保存当前连麦数据 到缓存失败:' . $e->getMessage(), 'error');
            return false;
        }

        return true;
    }

    /**
     * 从缓存中 获取当前连麦数据
     * @param int $liveRecordId
     * @param array $filed
     * @return bool|array|string
     */
    public function getNowRtcDataByCache(int $liveRecordId, array $filed = [])
    {
        try
        {
            return CacheManage::getInstance()->setCacheConfig($this->cacheModel2, $liveRecordId)->getCache($filed, 'details');
        }
        catch (\Exception $e)
        {
            Log::write('保存当前连麦数据 到缓存失败:' . $e->getMessage(), 'error');
            return false;
        }
    }

    /**
     * 从缓存中 删除当前连麦数据
     * @param int $liveRecordId
     * @return bool
     */
    public function delNowRtcDataByCache(int $liveRecordId)
    {
        try
        {
            CacheManage::getInstance()->setCacheConfig($this->cacheModel2, $liveRecordId)->rmCache('details');
        }
        catch (\Exception $e)
        {
            Log::write('保存当前连麦数据 到缓存失败:' . $e->getMessage(), 'error');
            return false;
        }
        return true;
    }

    /**
     * 获取连麦 网络状态描述
     * @param $liveNetworkStatus
     * @param $userNetworkStatus
     * @return array
     */
    public function getRtcNetworkStatusDesc(int $liveNetworkStatus, int $userNetworkStatus): array
    {
        if ($liveNetworkStatus === 1)
        {   // 主播状态好
            if ($userNetworkStatus === 1)
            {   // 用户状态好
                $liveNetWorkDes = '';
                $userNetWorkDes = '';
            } else
            {   // 用户状态不佳
                $liveNetWorkDes = '对方网络不佳';
                $userNetWorkDes = '当前网络不佳';
            }
        } else
        {   // 主播状态不佳
            if ($userNetworkStatus === 1)
            {   // 用户状态好
                $liveNetWorkDes = '当前网络不佳';
                $userNetWorkDes = '对方网络不佳';
            } else
            {   // 用户状态不佳
                $liveNetWorkDes = '当前网络不佳';
                $userNetWorkDes = '当前网络不佳';
            }
        }

        return [
            'live_network_desc' => $liveNetWorkDes,
            'user_network_desc' => $userNetWorkDes
        ];
    }
}
